// pages/work/index/index.js
var call = require("../../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    limit: 10,
    unreadTotal: 0, // 未读消息数
    keyName: '', // 搜索字段
    timer:null
  },
  // 去园区
  goPark(){
    wx.navigateTo({
      url: '/pages/park/index/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.cloud.init()
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: res => {
        let statusBarHeight = res.statusBarHeight // 状态栏的高度
        let navTop = menuButtonObject.top * 2
        let navObjWid = res.windowWidth - menuButtonObject.right + menuButtonObject.width // 胶囊按钮与右侧的距离 = windowWidth - right+胶囊宽度
        let navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
        let capsuleHeight = menuButtonObject.height * 2; //胶囊高度
        console.log(`导航栏总体高度${navHeight},胶囊距离顶部距离${navTop}.height,胶囊宽度(包括右边距离)${navObjWid}`)
        console.log(menuButtonObject)
        let contentHeight = capsuleHeight + navTop + 6 // 中间内容高度
        this.setData({
          navHeight,navTop,capsuleHeight,contentHeight
        })
      },
      fail(err) {
        console.log(err);
      }
    })

    //this.getListData()  // 获取消息数据
    this.setData({ currentPark: wx.getStorageSync('currentPark') })
  },
  // 获取消息数据
  getListData (status) {
    let page = this.data.page // 当前页
    let limit = this.data.limit // 每页条数
    let keyName = this.data.keyName // 搜索
    call.httpData('GET','/api/user/maillist',{ page,limit,keyName },
    (res) => {
      console.log(res,'获取消息数据')
      if(res.code == 1){
        let unreadTotal = res.data.unreadTotal
        this.setData({unreadTotal})
      }else{
        wx.showToast({ icon: 'none',title: res.msg })
      }
      this.setData({ pageStatus:true });
    },() => {
      doFail();
    })
  },
  // 首页
  goHome () {
    wx.redirectTo({
      url: '/pages/home/index'
    })
  },
  // 生活
  goLife () {
    wx.redirectTo({
      url: '/subPackages/pages/standard/home/index/index'
    })
  },
  // 消息
  goNew () {
    let userInfo = wx.getStorageSync('userInfo')  // 用户信息
    if(JSON.stringify(userInfo) == "{}"){
      let isSignIn = this.selectComponent('#isSignIn'); // 页面获取自定义组件实例
      isSignIn.isLogin(); // 通过实例调用组件事件
    }else{
      wx.redirectTo({
        url: '/pages/news/index/index'
      })
    }
  },
  // 我的
  goMy () {
    let userInfo = wx.getStorageSync('userInfo')  // 用户信息
    if(JSON.stringify(userInfo) == "{}"){
      let isSignIn = this.selectComponent('#isSignIn'); // 页面获取自定义组件实例
      isSignIn.isLogin(); // 通过实例调用组件事件
    }else{
      wx.redirectTo({
        url: '/pages/my/index/index'
      })
    }
  },
  // 未开放功能
  Public () {
    wx.showToast({
      icon: 'none',
      title: '加紧施工中...',
    })
  },
  onShow(){
    this.getUnread()
    this.setTimer()
  },
  getUnread(){
    wx.cloud.callContainer({
      "config": {
        "env": "prod-0g38oh8hf0e4d5ac"
      },
      "path": "/v1/chat/unReadNum",
      "header": {
        "X-WX-SERVICE": "springboot-069k"
      },
      "method": "get",
      "data": {
        "action": "inc"
      }
    }).then(res => {
      console.log(res.data, '定时器');
      if(res.data.code==1){
        // if (res.data.data > 0) {
          this.setData({
            'unreadTotal': res.data.data
          })
        // }
      }else{
        if (this.data.timer) {
          console.log('卸载定时器');
          clearInterval(this.data.timer);
          this.setData({
            timer: null
          })
        }
      }
    })
  },
  setTimer(){
    let _this = this
    let timer = setInterval(_this.getUnread, 5000)
    this.setData({
      timer
    })
  },
  onHide: function () {
    console.log('隐藏');
    if (this.data.timer) {
      console.log('卸载定时器');
      clearInterval(this.data.timer);
      this.setData({
        timer: null
      })
    }
  },
  onUnload: function () {
    console.log('卸载');
    if (this.data.timer) {
      console.log('卸载定时器');
      clearInterval(this.data.timer);
      this.setData({
        timer: null
      })
    }
  }
})